<?php
/**
 * actiondemo.php
 * An action that serves as a template for other actions.  As an action itself,
 * it demonstrates using the database, caching, and outputs its own code as a
 * result.
 *
 * Usage (in wiki page):
 *      {{actiondemo}}
 *
 * References
 *      http://docs.wikkawiki.org/UsingActions
 *
 * @package		Actions
 * @version		$Id$
 * @license		http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @filesource
 *
 * @uses	Wakka::LoadAll()
 * @uses	Wakka::GetConfigValue()
 *
 * @input	str   $foo optional: a sample action parameter
 * @input	bool  $show_code optional: whether or not to show debug output
 * @output      last page updated (from database)
 * @otuput      Debug info, including print out of this file
 */

// Defaults
$ActionDefaults = array(
    'show_code'     => 0,
    'foo'           => 'foo default value'
);

// Initialize
$show_code = $ActionDefaults['show_code'];
$here = sprintf('%s%s',
                 $this->GetConfigValue('base_url'),
                 $this->tag);
$user_status = 'viewer';

// Validate action parameters
if (  isset($_GET['show_code']) ) {
    $show_code = (bool) $_GET['show_code'];
}
if ( !isset($vars['foo']) ) {
    $vars['foo'] = $ActionDefaults['foo'];
}

// Get viewer status
if ( $user_ = $this->GetUser() ) {
    $user_status = 'user';
    if ( $this->IsAdmin($user_) ) {
        $user_status = 'admin';
    }
}



// MySQL Query
$sql_last_page_update = <<<HSQL
SELECT P.tag, P.time, P.latest, TIMEDIFF(Now(), P.time) as age,
    P.user, TIMEDIFF(Now(), U.signuptime) as user_age,
    Now() as now
FROM %s%s as P
LEFT JOIN %s%s as U on U.name = P.user
WHERE 1 = 1
    AND latest = 'Y'
ORDER BY time DESC, tag
LIMIT 1
HSQL;

// Run database query
$DbData = array();
$DbData['LastPageUpdate'] = $this->LoadAll( sprintf($sql_last_page_update,
                                 $this->GetConfigValue('table_prefix'), 'pages',
                                 $this->GetConfigValue('table_prefix'), 'users') );



// Prepare output
$output_t = <<<XHTML
<div class="action_output" id="actiontemplate_output">
    <div class="status">
        <strong>Usage:</strong>
        {{actiondemo}}
    </div>
    <div class="status">
        <strong>Last Page Update:</strong>
        <span class="hilight">%s<span>
        at <span class="hilight">%s<span>
        by <span class="hilight">%s<span>
    </div>
    <div class="status">
        <strong>Last File Update:</strong>
        <span class="hilight">%s<span>
    </div>
    <div class="status">
        <strong>Your Status (viewer, user, or admin):</strong>
        <span class="hilight">%s<span>
    </div>
    <div class="status">
        <strong>\$vars['foo']:</strong>
        <span class="hilight">%s<span>
    </div>
    <div class="actionbar">
        <a href="%s?show_code=1" class="%s">show code</a> |
        <a href="%s?show_code=0" class="%s">hide code</a>
    </div>
    <div style="display:%s;">
        <h4>Parameters</h4>
        <pre>%s</pre>

        <h4>Database Result</h4>
        <pre>%s</pre>

        <h4>Action Code</h4>
        <pre>%s</pre>
    </div>
</div>
XHTML;

$LastPage = $DbData['LastPageUpdate'][0];
$Parameters = array(
    '$vars'             => $vars,
    '$ActionDefaults'   => $ActionDefaults,

    # some debug values: comment out in production
    #'$_GET'             => $_GET,
    #'$_SERVER'          => $_SERVER,
    #'WakkaClass'        => $this
);

// Output
printf( $output_t,
        $LastPage['tag'], $LastPage['time'], $LastPage['user'],
        date('Y-m-d H:i:s', filemtime(__FILE__)),
        $user_status,
        $vars['foo'],
        $here, ( $show_code ) ? 'active' : 'normal',
        $here, ( ! $show_code ) ? 'active' : 'normal',
        $show_code ? 'block' : 'none',
        htmlentities(print_r($Parameters,1)),
        print_r($DbData,1),
        highlight_file(__FILE__, 1));
?>
